package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.ArticleMapper;
import com.jieshuibao.mapper.ArticleUserMapper;
import com.jieshuibao.mapper.CmsArticleMapper;
import com.jieshuibao.mapper.DiscussMapper;
import com.jieshuibao.model.db.ArticleUserExample;
import com.jieshuibao.model.db.ArticleUserExample.Criteria;
import com.jieshuibao.model.db.ArticleUserKey;
import com.jieshuibao.model.db.CmsArticle;
import com.jieshuibao.model.db.Discuss;
import com.jieshuibao.model.domain.Article;
import com.jieshuibao.model.domain.ArticleCategory;
import com.jieshuibao.model.domain.PagePhone;

@Service
public class ArticelService {
	@Resource
	private ArticleMapper articleMapper;
	@Resource
	private ArticleUserMapper articleUserMapper;
	@Resource
	private DiscussMapper discussMapper;
	@Resource
	private CmsArticleMapper cmsArticleMapper;

	public List<Article> getArticle(int pageIndex,int pageSize) {
		return articleMapper.selectArticel(pageIndex,pageSize);
	}
	//查询文章标签
	public List<ArticleCategory> getArticleCategory(String pid){
		return articleMapper.selectCategoryByPid(pid);
	}
	//根据文章id查一个
	public Article getArticleById(String articleId){
		return articleMapper.selectArticleById(articleId);
	}
	//收藏文章
	public void addArticleUser(ArticleUserKey articleUserKey){
		articleUserMapper.insertSelective(articleUserKey);
	}
	//查询收藏文章
	public ArticleUserKey getArticleUserKey(ArticleUserKey articleUserKey){
		return articleMapper.selectArticleUserById(articleUserKey);
	}
	//删除收藏文章
	public void delArticleUserKey(ArticleUserKey articleUserKey){
		articleUserMapper.deleteByPrimaryKey(articleUserKey);
	}
	//文章评论提交
	public void addDiscussAndUpdateArticle(Discuss discuss,CmsArticle cmsArticle){
		discussMapper.insertSelective(discuss);
		cmsArticleMapper.updateByPrimaryKeySelective(cmsArticle);
	}
	//收藏文章列表
	public List<Article> getCollectionArticle(PagePhone pagePhone){
		return articleMapper.selectCollection(pagePhone);
	}
	//根据文章标签查询文章
	public List<Article> getArticlesByCategoryId(PagePhone pagePhone){
		return articleMapper.selectArticleByCategoryId(pagePhone);
		
	}
	//最热文章
	public List<Article> getArticlesByHot(PagePhone pagePhone){
		return articleMapper.selectHotArticle(pagePhone);
		
	}
	//删除文章
	public void delAricle(String id){
		articleMapper.updateDelFlag(id);
		
	}
	//查看一个人下的所有文章
	public List<Article> getArticleByPhone(PagePhone pagePhone){
		return articleMapper.selectArticleByPhone(pagePhone);
	}
	//根据标题搜索文章
	public List<Article> getArticleByTitle(PagePhone pagePhone){
		return articleMapper.selectSearchArticle(pagePhone);
	}
	//查询一个文章及是否被收藏
	public Article getArticleAndIfCollectionById(String articleId,String phoneNumber){
		Article article = new Article();
		article = articleMapper.selectArticleById(articleId);
		if (article==null) {
			return article;
		}
		ArticleUserExample example = new ArticleUserExample();
		Criteria create = example.createCriteria();
		create.andUserPhoneNumEqualTo(phoneNumber);
		create.andArticleUuidEqualTo(articleId);
		List<ArticleUserKey> list = articleUserMapper.selectByExample(example);
		if(list != null && list.size() == 1){
			article.setIfCollection(1);
		}else{
			article.setIfCollection(0);
		}
		return article;
	}
}
